import base64
import io
import tkinter as tk
from PIL import Image, ImageTk, ImageSequence
import memory_pic
from tkinter import messagebox

gif = memory_pic.start_gif
xbox = memory_pic.xbox_ico

socket_value = 'https://server.halozero.online'


def play_animation():
    # 打开GIF图像文件
    image = Image.open(io.BytesIO(base64.b64decode(gif)))

    # 获取图像的所有帧
    frames = []
    for frame in ImageSequence.Iterator(image):
        frames.append(ImageTk.PhotoImage(frame))

    # 创建一个标签显示GIF图像
    label = tk.Label(window, image=frames[0])
    label.pack()

    # 播放动画
    def update_frame(frame_index):
        # 更新标签的图像
        label.configure(image=frames[frame_index])
        # 获取下一帧的索引
        next_frame_index = (frame_index + 1) % len(frames)
        # 在固定的时间间隔后调用更新函数
        window.after(20, update_frame, next_frame_index)

    # 开始动画
    update_frame(0)


def close_window():
    window.destroy()


def close_root():
    root.destroy()


# 创建一个Tkinter窗口
window = tk.Tk()
window.overrideredirect(True)
screenWidth = window.winfo_screenwidth()  # 屏幕宽度
screenHeight = window.winfo_screenheight()  # 屏幕高度
w = 400
h = 200
x = (screenWidth - w) / 2
y = (screenHeight - h) / 2
window.geometry("%dx%d+%d+%d" % (w, h, x, y))  # 距离屏幕左上角(400, 200)
# 在窗口中播放动画
play_animation()
# 运行Tkinter的事件循环
window.after(2000, close_window)
window.mainloop()


def submit_config():
    global socket_value
    socket_value = entry.get()
    # 在这里可以将 config_value 保存到文件或传递给其他部分进行处理
    messagebox.showinfo("配置已保存", f"已保存上报地址配置：{socket_value}")


# 创建主窗口
root = tk.Tk()
root.title("Halo监控配置")
label = tk.Label(root, text="默认地址: https://server.halozero.online")
label.pack(pady=5)
# 添加输入框和按钮
label = tk.Label(root, text="请在15s内输入上报地址:")
label.pack(pady=5)

entry = tk.Entry(root, width=40)
entry.pack(pady=5)

submit_button = tk.Button(root, text="保存配置", command=submit_config)
submit_button.pack(pady=5)

screenWidth = root.winfo_screenwidth()  # 屏幕宽度
screenHeight = root.winfo_screenheight()  # 屏幕高度
w = 400
h = 170
x = (screenWidth - w) / 2
y = (screenHeight - h) / 2
root.geometry("%dx%d+%d+%d" % (w, h, x, y))  # 距离屏幕左上角(400, 200)

root.after(15000, close_root)
root.mainloop()
